<?php
namespace Adminx\Controller;

class SubjectController extends AdminController {

	public function index(){

		$obj = M('Subject');
		$count = $obj->where($map)->count();

		import("Common.ORG.Page");
		$page = new \Page($count, 15);
		$show = $page->show();
		$list = $obj->where($map)->order('id desc')->limit($page->firstRow . ',' . $page->listRows)->select();
		foreach ($list as $key => $value) {
			$list[$key]['number']=M('Question')->where('sid='.$value['id'])->count();
		}
		$this->assign('list', $list);
		$this->assign('page', $show);
		$this->display();
    }

    #添加
	public function add() {
		if ($_POST) {
			$this->all_add('Subject',U('Subject/index'));
		}else{
			$this->display();
		}		
	}

	#编辑
	public function edit() {
		if ($_POST) {
			$this->all_save('Subject',U('Subject/index'));
		}else{
			$id = (int) $_GET['id'];
			if (!isset ($id)) {
				$this->error('参数错误');
			}
			$obj = M('Subject');
			$list = $obj->where("id=$id")->find();
			if (!$list) {
				$this->error('信息不存在');
			} else {
				$this->assign('list', $list);
				$this->display();
			}
		}
	}

	#删除
	public function del() {
		$id=I('post.selectedids');
		if($id==''){
			$this->error('您没有选择任何信息！');
		}else{
			foreach ($id as $v) {
				$obj = M('Subject');
				$where['id'] = $v;
				$obj->where($where)->delete();
				unset($where);
				$obj = M('Question');
				$where['sid'] = $v;
				$obj->where($where)->delete();
			}
			$this->success('删除成功','reload');
		}
	}

	#清空票数
	public function clear(){
		$id = I('get.id');
		$map['sid']=$id;
		$list = M('Question')->where($map)->setField('poll','0');
		echo 1;
	}
}
?>